<template>
  <div class="my_form">
    <el-form ref="form" :model="form.bindModle" label-width="80px">
      <el-form-item
        v-for="item in form.formList"
        :label="item.label"
        :key="item.index"
        :rules="item.rules"
        :prop="item.prop"
      >
        <!-- 输入框 -->
        <el-input
          v-if="item.type === 'input'"
          v-model="form.bindModle[item.prop]"
          :size="item.size"
          :placeholder="item.placeholder"
          :style="{width: item.width}"
        ></el-input>

        <!-- 下拉框 -->
        <el-select
          clearable
          v-if="item.type==='select'"
          :placeholder="item.placeholder"
          v-model="form.bindModle[item.prop]"
          :style="{width: item.width}"
          :size="item.size"
          @change="item.change && item.change(form.bindModle[item.prop])"
        >
          <el-option v-for="op in item.options" :label="op.label" :value="op.value" :key="op.value"></el-option>
        </el-select>

        <!-- 单选 -->
        <el-radio-group
          v-if="item.type==='radio'"
          v-model="form.bindModle[item.prop]"
          @change="item.change && item.change(form.bindModle[item.prop])"
        >
          <el-radio v-for="ra in item.radios" :label="ra.value" :key="ra.value">{{ra.label}}</el-radio>
        </el-radio-group>

        <!-- 多选 -->
        <el-checkbox-group
          v-if="item.type==='checkbox'"
          :style="{width: item.width}"
          v-model="form.bindModle[item.prop]"
          @change="item.change && item.change(form.bindModle[item.prop])"
        >
          <el-checkbox v-for="ch in item.checkboxs" :label="ch.value" :key="ch.value">{{ch.label}}</el-checkbox>
        </el-checkbox-group>

        <!-- 日期 -->
        <el-date-picker
          v-if="item.type==='date'"
          :placeholder="item.placeholder"
          v-model="form.bindModle[item.prop]"
          :value-format="item.valueFormat"
        ></el-date-picker>

        <!-- 时间 -->
        <el-time-select v-if="item.type==='time'" v-model="form.bindModle[item.prop]"></el-time-select>

        <!-- 日期时间 -->
        <el-date-picker
          v-if="item.type==='dateTime'"
          type="datetime"
          v-model="form.bindModle[item.prop]"
          :value-format="item.valueFormat"
        ></el-date-picker>

        <!-- 开关 -->
        <el-switch v-if="item.type==='switch'" v-model="form.bindModle[item.prop]"></el-switch>
      </el-form-item>
      <div v-if="form.isHandle">
        <el-button type="primary" size="small" @click="handleSub">提交</el-button>
        <el-button type="primary" size="small" @click="handleRest">重置</el-button>
      </div>
    </el-form>
  </div>
</template>


<script>
export default {
  props: {
    form: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {};
  },
  methods: {
    handleSub() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$emit("handleSub", this.form.bindModle);
        } else {
          console.log("error submit!!111111111111111111");
          return false;
        }
      });
    },
    handleRest() {
      this.$refs["form"].resetFields();
    }
  }
};
</script>